<template>
	<view>
		<view class="gaps">

		</view>

		<view class="goods-panel">
			<view v-for="item in goods" :key="item" @click="limitClick(item)" class="goods-item">
				<view slot="header">
					<image class="slot-image" :src="item.goodsImg || '/static/logo.png'" mode="scaleToFill"
						@click="changeLimit(item)"></image>
				</view>
				<view slot="body">
					<view class="slot-body">
						<view>{{item.goodsName}}</view>
						<view>{{priceTransform(item.goodsPrice)}}/{{item.spec}}</view>
						<view>商品状态:{{item.goodsState === 0 ? '下架' : '上架'}}</view>
						<view>起售数量:{{item.minSaleNum}}</view>
						<view>库存:{{item.stock}}</view>
						<view>限购状态:{{item.limitState === 0 ? '关闭': '开启'}}</view>
						<view>下单次数:{{item.times}}</view>
						<view>每次下单个数:{{item.num}}</view>
					</view>
				</view>
			</view>
		</view>


		<view class="gaps">

		</view>
		<button type="primary" @click="addGoodsClick">添加商品</button>


		<uni-popup ref="limitPop" type="center" :is-mask-click="false">
			<view class="limit-form">
				<view class="goods-name">{{tmpName}}</view>
				<uni-forms ref="baseForm" :modelValue="limitForm">
					<uni-forms-item label="下单次数" required>
						<uni-easyinput type="number" v-model="limitForm.times" placeholder="请输入下单次数" />
					</uni-forms-item>
					<uni-forms-item label="下单个数" required>
						<uni-easyinput type="number" v-model="limitForm.num" placeholder="请输入下单个数" />
					</uni-forms-item>
					<uni-forms-item label="是否开启" required>
						<uni-data-checkbox v-model="limitForm.state" :localdata="limitStates"></uni-data-checkbox>
					</uni-forms-item>
				</uni-forms>
				<view class="form-btn">
					<view>
						<button type="default" @click="cancelPop">取消</button>
					</view>
					<view>
						<button type="primary" @click="confirmLimit">确定</button>
					</view>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

<script>
	import {
		getGoodsList,
		updateLimit
	} from '@/api/shops/limitSale/limitSale.js'
	import {
		transform
	} from '@/minix/mixin.js'
	export default {
		mixins: [transform],
		data() {
			return {
				shopId: '',
				goods: [],
				limitForm: {},
				limitStates: [{
					"value": 0,
					"text": "关闭"
				}, {
					"value": 1,
					"text": "开启"
				}],
				tmpName:''
			}
		},
		onLoad(options) {
			this.shopId = options.shopId
			this.init()
		},
		methods: {
			init() {
				getGoodsList().then((res) => {
					this.goods = res
				})
			},
			addGoodsClick() {
				let _this = this;
				uni.navigateTo({
					url: '/subpkg/shops/marketAddGoods/marketAddGoods?shopId=' + _this.shopId + '&type=1', // 1表示限购
					events: {
						selectGoods: function(data) {
							if (data.data) {
								_this.init()
							}
						}
					}
				})
			},
			limitClick(item) {
				this.tmpName = item.goodsName;
				this.limitForm = {
					goodsId: item.goodsId,
					num: item.num,
					times: item.times,
					state: item.limitState
				}
				this.$refs.limitPop.open()
			},
			cancelPop() {
				this.$refs.limitPop.close()
			},
			confirmLimit() {
				let _this = this
				uni.showModal({
					title: '确定修改吗',
					success: (res) => {
						if (res.confirm) {
							updateLimit(_this.limitForm).then((r) => {
								let g = _this.goods.filter(e => {
									return e.goodsId == _this.limitForm.goodsId
								})

								g.num = _this.limitForm.num
								g.times = _this.limitForm.times
								g.limitState = _this.limitForm.state

								this.$refs.limitPop.close()

								uni.showToast({
									title: '修改成功',
									icon: 'none'
								})

							})
						}
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	.goods-item{
		display: flex;
		flex-direction: row;
		border-bottom: 1px solid #555555;
		margin-bottom: 5px;
		padding: 10px;
	}
	.slot-image {
		width: 100px;
		height: 100px;
		border-radius: 8px;
	}

	.slot-body {
		margin-left: 10px;
		flex: 1;
	}

	.limit-form {
		background-color: #fff;
		padding: 10px;
	}
	.goods-name{
		text-align: center;
	}

	.form-btn {
		display: flex;
		flex-direction: row;
		justify-content: space-around;
		align-items: center;
	}
</style>